package cn.wit.test;

import java.io.File;
import java.util.Arrays;

import org.junit.Test;


public class DemoRegex {
	
	public static void main(String[] args) {
		String Path = "E:\\项目\\Excel";
		File dir = new File(Path);
		listDir(dir);
//		System.out.println(isExcle1(Path));
		
	}
	
	public static void listDir(File dir) {
		
		//获取当前目录下的所有子目录
		File[] file = dir.listFiles();
		
		for(File f:file) {
			if(f.isDirectory()) {
				//如果是目录，就重复调用
				listDir(f);
			}else {
				if(isExcle2(f.getName())) {
					System.out.println(f.getAbsolutePath()+" ");
				}
			}
		}
	}
	
	//使用正则表达式处理
	public static boolean isExcle2(String fileName) {
		
		return fileName.matches(".*\\.(xlsx|docx)");//斜杠放在字符串里面，需要使用两个斜杠
	}
	
	//取出后缀处理
	public static boolean isExcle1(String fileName) {

		int c = fileName.lastIndexOf(".");
		
		String suffix = fileName.substring(c+1);
		
		if("xlsx".equals(suffix)) {
			return true;
		}else {
			return false;
		}
	}
	
	//测试split
	//把一段英文里面的最后一个单词取出来
	@Test
	public void test1() {
		String str = "public static boolean isExcle2";
		
		String[] strs = str.split("[ ,.]");
		
		System.out.println(Arrays.toString(strs));
	}
	
	//测试replaceAll
	@Test
	public void test2() {
		
		String str = "aaa881ajajn1";
		
		//替换后，是创建给了一个新的对象，其原来的对象不变，
		String str1 = str.replaceAll("[0-9]","");
		
		System.out.println(str1);
		
	}
	
	@Test
	public void test3() {
		
		//以abc开头，后面接任意字符
		String regex = "^abc.*";
		System.out.println("abc".matches(regex));
		System.out.println("ababc".matches(regex));
		System.out.println("abcd".matches(regex));
	}
	
	@Test
	public void test4() {
		
		//以任意字符开头，以abc结尾
		String regex = ".*abc$";
		System.out.println("abc".matches(regex));
		System.out.println("ababc".matches(regex));
		System.out.println("abcd".matches(regex));
	}
	
	@Test
	public void test5() {
		
		//先定义一个正则表达式
		String regex = "abc*d";
		
		System.out.println("abcd".matches(regex));
		System.out.println("abd".matches(regex));
		System.out.println("abccd".matches(regex));
		System.out.println("abcabcd".matches(regex));
		
	}
	
	@Test
	public void test6() {
		
		//先定义一个正则表达式
		String regex = "(abc)*d";
		
		System.out.println("d".matches(regex));
		System.out.println("abcd".matches(regex));
		System.out.println("abd".matches(regex));
		System.out.println("abccd".matches(regex));
		System.out.println("abcabcd".matches(regex));
		
	}
	
	@Test
	public void test7() {
		String regex = ".+\\.(png|gif|jpg|jpeg)$";
		
		String regex1 = "[abc]";
		String regex2 = "(abc)";
		
		String regex3 = "[A-Za-z_0-9]";
		
		System.out.println("a".matches(regex3));
		
		System.out.println("a".matches(regex2));
		System.out.println("a".matches(regex1));
		
		System.out.println("a.jpg".matches(regex));
		System.out.println(".jpg".matches(regex));
		System.out.println("a.jp".matches(regex));
	}

	@Test
	public void test8() {
		
		//密码必须是6到18位
//		String	regex = "\\w{6,18}";
//		
//		System.out.println("123456".matches(regex));
//		System.out.println("1234".matches(regex));
//		System.out.println("password".matches(regex));
//		System.out.println("ab$b".matches(regex));
//		System.out.println("abababbahabahahahaha".matches(regex));
//		System.out.println("abcdefg".matches(regex));
//		
		//密码是6-18位，必须含数字
//		String regex = "(?=.*\\d.*)\\w{6,18}";
//		System.out.println("a3c".matches(regex));
//		System.out.println("abcd1234".matches(regex));
//		System.out.println("absjhsghs".matches(regex));
		
		//密码是6-18位，必须含数字,还得必须大写字母开头
		String regex = "(?=^[A-Z])(?=.*\\d.*)\\w{6,18}";
		System.out.println("a3c".matches(regex));
		System.out.println("abcd1234".matches(regex));
		System.out.println("absjhsghs".matches(regex));
		System.out.println("Assjhajhua6".matches(regex));
		
		//^[A-Z]\w{5,17},断言会影响程序性能
		
	}
	
	@Test
	public void test9() {
	
		//非负整数
//		String regex = "[1-9][0-9]*|0";
//		
//		System.out.println("12".matches(regex));
//		System.out.println("01".matches(regex));
//		System.out.println("-1".matches(regex));
//		System.out.println("1".matches(regex));
//		System.out.println("0".matches(regex));
		
		//整数
		String regex = "[+-]?[1-9]\\d*|0";
		System.out.println("123".matches(regex));
		System.out.println("+123".matches(regex));
		
	}
}
